/*** Eclipse Class Decompiler plugin, copyright (c) 2012 Chao Chen (cnfree2000@hotmail.com) ***/
package com.feiyue.helper;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Handler;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.feiyue.BookCatologActivity;
//import com.feiyue.basic.reader.PageActivity;
import com.feiyue.basic.reader.dao.FileRecordDao;
import com.feiyue.basic.reader.dao.SettingDao;
import com.feiyue.basic.reader.dialog.LoadingDialog;
import com.feiyue.basic.reader.pojo.FileRecord;
import com.feiyue.basic.server.db.ChapterDBHandler;
import com.feiyue.basic.server.net.NovelInfo;
import com.feiyue.basic.server.parser.Chapter;
import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Runnable2chapterlist implements Runnable {
	public static boolean isupdaterunning = false;
	private String author;
	private BookCatologActivity bookCatologActivity;
	private int bookid;
	private String bookname;
	private int categoryid;
	private String categoryname;
	private int chapterNum = 0;
	private List<Chapter> chapterlist;
	private LinearLayout contentlinearlayout;
	private FileRecordDao fileRecordDao;
	private List<FileRecord> fileRecordlist = new ArrayList();
	private String introduction;
	private String issuestate;
	private PullToRefreshListView list;
	private BookCatologAdapter listItemAdapter;
	private LoadingDialog loadingDialog;
	private SettingDao settingDao;
	private TextView textViewcount;

	public Runnable2chapterlist(BookCatologActivity paramBookCatologActivity,
			TextView paramTextView, LinearLayout paramLinearLayout,
			LoadingDialog paramLoadingDialog,
			PullToRefreshListView paramPullToRefreshListView,
			String paramString1, int paramInt1, String paramString2,
			int paramInt2, String paramString3, String paramString4,
			String paramString5) {
		this.bookCatologActivity = paramBookCatologActivity;
		this.textViewcount = paramTextView;
		this.contentlinearlayout = paramLinearLayout;
		this.loadingDialog = paramLoadingDialog;
		this.list = paramPullToRefreshListView;
		this.categoryname = paramString1;
		this.categoryid = paramInt1;
		this.bookname = paramString2;
		this.bookid = paramInt2;
		this.author = paramString3;
		this.introduction = paramString4;
		this.issuestate = paramString5;
	}

	private static String i2s(int i, int j)
    {
        String s = String.valueOf(i);
        if (s.length() < j) goto _L2; else goto _L1
_L1:
        return s;
_L2:
        char ac[];
        int k;
        ac = new char[j];
        k = 0;
_L5:
        if (k < j - s.length()) goto _L4; else goto _L3
_L3:
        if (k < j)
            break MISSING_BLOCK_LABEL_63;
        s = new String(ac);
          goto _L1
_L4:
        ac[k] = '0';
        k++;
          goto _L5
        ac[k] = s.charAt(-1 + (j - k));
        k++;
          goto _L3
    }

	public int getChapterNum() {
		return this.chapterNum;
	}

	public void run()
    {
        try
        {
            ChapterDBHandler chapterdbhandler = new ChapterDBHandler(bookCatologActivity);
            chapterlist = chapterdbhandler.getChapterList(bookCatologActivity, categoryid, bookid);
            chapterdbhandler.destroy();
        }
        catch (Exception exception) { }
        if (chapterlist != null && chapterlist.size() != 0) goto _L2; else goto _L1
_L1:
        Toast.makeText(bookCatologActivity, "没有获取到章节内容，可能是因为网络不通", 1).show();
        if (loadingDialog.isShowing())
            loadingDialog.dismiss();
_L4:
        return;
_L2:
        fileRecordDao = new FileRecordDao(bookCatologActivity);
        settingDao = new SettingDao(bookCatologActivity);
        fileRecordlist = fileRecordDao.getList(bookid);
        if (fileRecordlist.size() > 0)
            chapterNum = ((FileRecord)fileRecordlist.get(0)).getChapterNum();
        fileRecordDao.close();
        settingDao.close();
        contentlinearlayout.setVisibility(0);
        if (loadingDialog.isShowing())
            loadingDialog.dismiss();
        int i = chapterlist.size();
        textViewcount.setText((new StringBuilder(String.valueOf(chapterlist.size()))).toString());
        int j = String.valueOf(i).length();
        ArrayList arraylist = new ArrayList();
        int k = 0;
        do
        {
label0:
            {
                if (k < i)
                    break label0;
                BookCatologActivity bookcatologactivity = bookCatologActivity;
                String as[] = new String[2];
                as[0] = "ItemNum";
                as[1] = "ItemTile";
                int ai[] = new int[2];
                ai[0] = 0x7f060093;
                ai[1] = 0x7f060094;
                listItemAdapter = new BookCatologAdapter(bookcatologactivity, arraylist, 0x7f03000e, as, ai, chapterlist, chapterNum);
                list.setOnRefreshListener(new com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener() {

                    final Runnable2chapterlist this$0;

                    public void onRefresh(PullToRefreshBase pulltorefreshbase)
                    {
                        String s = DateUtils.formatDateTime(bookCatologActivity, System.currentTimeMillis(), 0x80011);
                        pulltorefreshbase.getLoadingLayoutProxy().setLastUpdatedLabel(s);
                        (new GetDataTask(null)).execute(new Void[0]);
                    }

            
            {
                this$0 = Runnable2chapterlist.this;
                super();
            }
                });
                list.setAdapter(listItemAdapter);
                ((ListView)list.getRefreshableView()).setSelection(-2 + chapterNum);
                list.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {

                    final Runnable2chapterlist this$0;

                    public void onItemClick(AdapterView adapterview, View view, int l, long l1)
                    {
                        int i1 = l - 1;
                        Intent intent = new Intent(bookCatologActivity, com/feiyue/basic/reader/PageActivity);
                        intent.putExtra("categoryname", categoryname);
                        intent.putExtra("categoryid", categoryid);
                        intent.putExtra("bookname", bookname);
                        intent.putExtra("bookid", bookid);
                        intent.putExtra("author", author);
                        intent.putExtra("introduction", introduction);
                        intent.putExtra("issuestate", issuestate);
                        intent.putExtra("chapterid", ((Chapter)chapterlist.get(i1)).getId());
                        intent.putExtra("chapterNum", i1);
                        if (chapterNum != i1)
                        {
                            chapterNum = i1;
                            (new Handler()).postDelayed(new Runnable() {

                                final 2 this$1;

                                public void run()
                                {
                                    listItemAdapter.setCurrentPosition(chapterNum);
                                    list.setAdapter(listItemAdapter);
                                    ((ListView)list.getRefreshableView()).setSelection(-2 + chapterNum);
                                }

                    
                    {
                        this$1 = 2.this;
                        super();
                    }
                            }, 1000L);
                        }
                        intent.setFlags(0x20000);
                        bookCatologActivity.startActivity(intent);
                        bookCatologActivity.finish();
                    }


            
            {
                this$0 = Runnable2chapterlist.this;
                super();
            }
                });
            }
            if (true)
                continue;
            HashMap hashmap = new HashMap();
            i2s(k, j);
            Object aobj[] = new Object[2];
            aobj[0] = Integer.valueOf(k + 1);
            aobj[1] = ((Chapter)chapterlist.get(k)).getName();
            String.format("第%d %s", aobj);
            hashmap.put("ItemNum", (new StringBuilder(String.valueOf(k + 1))).toString());
            hashmap.put("ItemTile", ((Chapter)chapterlist.get(k)).getName());
            arraylist.add(hashmap);
            k++;
        } while (true);
        if (true) goto _L4; else goto _L3
_L3:
    }

	public void set(List<Chapter> paramList) {
		this.chapterlist = paramList;
	}

	public void update() {
		isupdaterunning = true;
		this.chapterlist = NovelInfo.getChapterList(this.bookCatologActivity,
				NovelInfo.getUrl2Chapterlist(this.bookid), this.categoryid,
				this.bookid, "update", this.bookCatologActivity.handler);
		set(this.chapterlist);
		isupdaterunning = false;
	}

	private class GetDataTask extends AsyncTask<Void, Void, String[]> {
		private boolean dotips = false;

		protected String[] doInBackground(Void[] paramArrayOfVoid)
    {
//      if (!(Runnable2chapterlist.isupdaterunning))
//      {
//        Runnable2chapterlist.this.update();
//        this.dotips = true;
//      }
//      while (true)
//      {
//        return null;
//        try
//        {
//          label26: 
//          do
//            Thread.sleep(2000L);
//          while (Runnable2chapterlist.isupdaterunning);
//        }
//        catch (InterruptedException localInterruptedException)
//        {
//          break label26:
//        }
//      }
		    
		    return null;
    }

		protected void onPostExecute(String[] paramArrayOfString) {
			if (this.dotips) {
				Toast.makeText(Runnable2chapterlist.this.bookCatologActivity,
						"更新已完成", 1).show();
				Intent localIntent = new Intent("CatologUpdate");
				localIntent
						.putExtra("BookID", Runnable2chapterlist.this.bookid);
				Runnable2chapterlist.this.bookCatologActivity
						.sendBroadcast(localIntent);
			}
			Runnable2chapterlist.this.list.onRefreshComplete();
			super.onPostExecute(paramArrayOfString);
		}
	}
}